<?php

/**
* AdminAuthTest.php
*
* Test auth issues 
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminAuthTest extends WebTestCase
{


    private $dbh = null;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');

        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
  
        $this->dbh = new PDO($options['dsn_string']);        
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url."?cmd=AdminGuideList");
    }

    public function testLoginForm()
    {
        $this->get($this->url."?cmd=AdminGuideList");
        $this->assertText("Podaj login i hasło!"); 
        $this->assertText("Login"); 
        $this->assertText("Hasło"); 
        $this->assertField('username'); 
        $this->assertField('password');
    } 

    public function testLogin()
    {

        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertText("Lista tras"); 
  


    } 

    public function testLoginNoPassword()
    {

        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', '');
        $this->click("Login");
        $this->assertText("Błędny login lub hasło!");
    } 


    public function testLogout()
    {

        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertText("Lista tras");
        $this->get($this->url."?cmd=AdminGuideList&logout=1");
        $this->assertText("Lista tras");
        $this->get($this->url."?cmd=AdminGuideList");
        $this->assertText("Podaj login i hasło!"); 

    } 

   
    public function testRegister()
    {
  
        $this->get($this->url."?cmd=AuthRegister");

        $this->assertText("Podaj login i hasło. Użyj działającego adresu e-mail jako login."); 
        $this->assertText("Login"); 
        $this->assertText("Hasło"); 
        $this->assertText("Powtórz hasło"); 

        $this->assertField('username'); 
        $this->assertField('password');
        $this->assertField('passwordRepeat');

        $username =  md5( uniqid( rand(), true ) )."@pl.pl";;
        $password =  md5( uniqid( rand(), true ) );

        $this->setFieldByName("username", $username);
        $this->setFieldByName("password", $password);
        $this->setFieldByName("passwordRepeat", $password);
        $this->click("Dodaj");
        $this->assertText("Użytkownik dodany.");
        
        $userList = array();
        $userList = AuthSingleton::instance()->listUsers();

        $found = false;
        foreach( $userList as $value )
        {
            if ( $value['user'] == $username )
            {
                $found = true;
            }
        }    
        
        $this->assertTrue($found, "User was not found : ".$username);
        $this->assertTrue(AuthSingleton::instance()->removeUser($username,$password));
    } 

    public function testRegisterNotUnique()
    {
        
        $this->get($this->url."?cmd=AuthRegister");

        $this->assertText("Podaj login i hasło. Użyj działającego adresu e-mail jako login."); 
        $this->assertText("Login"); 
        $this->assertText("Hasło"); 
        $this->assertText("Powtórz hasło"); 

        $this->assertField('username'); 
        $this->assertField('password');
        $this->assertField('passwordRepeat');


        $this->setFieldByName("username", $this->username);
        $this->setFieldByName("password", $this->password);
        $this->setFieldByName("passwordRepeat", $this->password);
        $this->click("Dodaj");
        $this->assertText("Użytkownik już istnieje w bazie.");
    } 

    public function testRegisterPasswordNotEqual()
    {
        
        $this->get($this->url."?cmd=AuthRegister");

        $this->assertText("Podaj login i hasło. Użyj działającego adresu e-mail jako login."); 
        $this->assertText("Login"); 
        $this->assertText("Hasło"); 
        $this->assertText("Powtórz hasło"); 

        $this->assertField('username'); 
        $this->assertField('password');
        $this->assertField('passwordRepeat');

        $username =  md5( uniqid( rand(), true ) )."@pl.pl";;
        $password =  md5( uniqid( rand(), true ) );

        $this->setFieldByName("username", $username);
        $this->setFieldByName("password", $password);
        $this->setFieldByName("passwordRepeat", "Magic");
        $this->click("Dodaj");
        $this->assertText("Podane hasła nie są identyczne.");

        $userList = array();
        $userList = AuthSingleton::instance()->listUsers();

        $found = false;
        foreach( $userList as $value )
        {
            if ( $value['user'] == $username )
            {
                $found = true;
            }
        }    
        
        $this->assertFalse($found, "User was added");

    } 



    public function tearDown() 
    {     
        AuthSingleton::instance()->removeUser($this->username,$this->password);
    }
    
}
?>
